package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.ProgressNodeDetail;
import com.ruoyi.system.domain.ProgressNodeItem;
import com.ruoyi.system.domain.ProgressNodeMessage;
import com.ruoyi.system.mapper.ProgressNodeDetailMapper;
import com.ruoyi.system.mapper.ProgressNodeItemMapper;
import com.ruoyi.system.service.IProgressNodeDetailService;
import com.ruoyi.system.service.INodeMessageService;
import java.util.Date;
import java.util.List;

@Service
public class ProgressNodeDetailServiceImpl implements IProgressNodeDetailService {
    @Autowired
    private ProgressNodeDetailMapper nodeDetailMapper;

    @Autowired
    private ProgressNodeItemMapper nodeItemMapper;

    @Autowired
    private INodeMessageService nodeMessageService;

    @Override
    @Transactional
    public int insertNodeDetail(ProgressNodeDetail detail) {
        // 检查该节点是否已经有明细
        ProgressNodeDetail existDetail = nodeDetailMapper.selectNodeDetailByNodeId(detail.getNodeId());
        if (existDetail != null) {
            throw new ServiceException("该节点已存在明细信息，不能重复创建");
        }

        int rows = nodeDetailMapper.insertNodeDetail(detail);

        // 只有当设置了初始负责人时才生成消息
        if (rows > 0 && detail.getManagerId() != null) {
            String message = "节点初始化，负责人: " + detail.getManagerName();

            // 保存消息
            ProgressNodeMessage nodeMessage = new ProgressNodeMessage();
            nodeMessage.setNodeId(detail.getNodeId());
            nodeMessage.setDetailId(detail.getDetailId());
            nodeMessage.setMessage(message);
            nodeMessageService.insertNodeMessage(nodeMessage);
        }

        // 处理工件信息
        if (detail.getItems() != null && !detail.getItems().isEmpty()) {
            for (ProgressNodeItem item : detail.getItems()) {
                item.setDetailId(detail.getDetailId());
            }
            nodeItemMapper.batchInsertNodeItems(detail.getItems());
        }

        return rows;
    }

    @Override
    @Transactional
    public int updateNodeDetail(ProgressNodeDetail detail) {
        // 获取原数据
        ProgressNodeDetail oldDetail = nodeDetailMapper.selectNodeDetailById(detail.getDetailId());
        if (oldDetail == null) {
            throw new ServiceException("节点明细不存在");
        }

        // 确保是更新同一个节点的明细
        if (!oldDetail.getNodeId().equals(detail.getNodeId())) {
            throw new ServiceException("不能修改其他节点的明细信息");
        }

        // 生成动态消息
        StringBuilder message = new StringBuilder();

        // 检查负责人变更 - 只在从空变为非空时生成消息
        if (detail.getManagerId() != null &&
            (oldDetail.getManagerId() == null || oldDetail.getManagerId() == 0)) {
            message.append("负责人设置为: ").append(detail.getManagerName());
        }

        // 检查状态变更 - 只在从空变为非空时生成消息
        if (detail.getStatus() != null && !detail.getStatus().isEmpty() &&
            (oldDetail.getStatus() == null || oldDetail.getStatus().isEmpty())) {
            if (message.length() > 0) {
                message.append(", ");
            }
            message.append("状态设置为: ").append(detail.getStatus());

            // 如果状态为"已完成"，自动设置完成时间
            if ("已完成".equals(detail.getStatus())) {
                detail.setCompleteTime(new Date());
            }
        }

        // 设置动态消息
        if (message.length() > 0) {
            String messageStr = message.toString();

            // 保存消息
            ProgressNodeMessage nodeMessage = new ProgressNodeMessage();
            nodeMessage.setNodeId(detail.getNodeId());
            nodeMessage.setDetailId(detail.getDetailId());
            nodeMessage.setMessage(messageStr);
            nodeMessageService.insertNodeMessage(nodeMessage);
        }

        return nodeDetailMapper.updateNodeDetail(detail);
    }

    @Override
    public ProgressNodeDetail selectNodeDetailById(Long detailId) {
        ProgressNodeDetail detail = nodeDetailMapper.selectNodeDetailById(detailId);
        if (detail != null) {
            detail.setItems(nodeItemMapper.selectNodeItemsByDetailId(detailId));
        }
        return detail;
    }

    @Override
    public ProgressNodeDetail selectNodeDetailByNodeId(Long nodeId) {
        ProgressNodeDetail detail = nodeDetailMapper.selectNodeDetailByNodeId(nodeId);
        if (detail != null) {
            detail.setItems(nodeItemMapper.selectNodeItemsByDetailId(detail.getDetailId()));
        }
        return detail;
    }

    @Override
    public int insertNodeItem(ProgressNodeItem item) {
        return nodeItemMapper.insertNodeItem(item);
    }

    @Override
    public int batchInsertNodeItems(List<ProgressNodeItem> items) {
        return nodeItemMapper.batchInsertNodeItems(items);
    }
}
